package model.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import model.configuration.ConfigData;

/**
 * Connection pool for database
 * @author Potseluev
 *
 */
public class ConnectionPool {
	
	
	//private static Connection _connection = null;		// 
	private static InitialContext initContext = null;
	private static DataSource dataSource = null;
	private static Context webContext = null;
	
	public static Connection getConnection() throws Exception {
		
		if (initContext == null) {
			initContext = new InitialContext();
			webContext = (Context)initContext.lookup(ConfigData.CONTEXTNAME);
		}
		
		if (dataSource == null) {
			dataSource = (DataSource) webContext.lookup(ConfigData.DATASOURCE);
		}
		
		return dataSource.getConnection();
	}
	
//	private static Connection initConnection() throws Exception {
//		Class.forName(ConfigData.DRIVERCLASSNAME);
//		//System.setProperty("java.net.preferIPv4Stack" , "true"); //force use ipv4. Uses for Oracle virtual box
//		Properties props = new Properties();
//		props.setProperty("user",ConfigData.CONNECTIONUSER);
//		props.setProperty("password",ConfigData.CONNECTIONPASSWORD);
//		//props.setProperty("ssl","true");
//		return DriverManager.getConnection(ConfigData.CONNECTIONSTRING, props);
//	}

}
